' This file is part of the SunDLL.  The SunDLL is free software: you can
' redistribute it and/or modify it under the terms of the GNU General Public
' License as published by the Free Software Foundation, version 2.
'
' This program is distributed in the hope that it will be useful, but WITHOUT
' ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
' FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
' details.
'
' You should have received a copy of the GNU General Public License along with
' this program; if not, write to the Free Software Foundation, Inc., 51
' Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'
' Copyright David R. Pratten


Function xwrap(ByVal id As String, ByVal value As String) As String
Function = "<"+Trim$(id)+">"+value+"</"+Trim$(id)+">"
End Function

Function xerror(Errno As Long) As String
Function = Str$(Errno)+xwrap($TagError,Str$(Errno))
End Function

Function xcount(xid As String) As Long

End Function


Function xunwrapext(ByVal xid As String, ByVal xstring As String, startpos As Long,foundstart As Long ) As String
' Assumes no imbedded "<" or ">" and no quoting on strings
' finds first tagged value
' No error checking done should find start token, end token and then take middle
Dim i&
Dim j&
Dim bpos&
Dim blen&
Dim epos&
Dim elen&
Dim xvalue As String
Dim eid As String
Dim pattstart As String
Dim pattend As String

If startpos < 1 Then
   Function = $BadParameter
   Exit Function
End If

xid = Trim$(xid)

If xid = "" Then
    pattstart = "<[^/>]+>"
    pattend = "</.+>"
Else
    pattstart = "<"+xid+"[^>]*>"
    pattend = "</"+xid+"[^>]*>"
End If
RegExpr pattstart In xstring At startpos To bpos&, blen&
If bpos& = 0 And blen& = 0 Then
   Function = $BadParameter
   Exit Function
End If

RegExpr pattend In xstring+" " At startpos To epos&, elen&
If epos& = 0 And elen& = 0 Then
   Function = $BadParameter ' ie return null for absent
   Exit Function
End If


xid = Rtrim$(Mid$(xstring,bpos&+1,blen&-2))
' find first whitespace
RegExpr "^.+\b" In xid To i&, j&
'#Debug Print Str$(i&)+Str$(j&)+xid
xid = Mid$(xid,i&,j&-i&+1)
'#Debug Print Str$(i&)+Str$(j&)+xid

eid = Rtrim$(Mid$(xstring,epos&+2,elen&-3))
' find first whitespace
RegExpr "^.+\b" In eid To i&, j&
'#Debug Print Str$(i&)+Str$(j&)+eid
eid = Mid$(eid,i&,j&-i&+1)
'#Debug Print Str$(i&)+Str$(j&)+eid

'#Debug Print xid+Rtrim$(Mid$(xstring,epos&+2,elen&-3))

If xid <> eid Then 'badly formed
   xid = $BadParameter
   Function = $BadParameter
   Exit Function
End If
startpos = epos&+elen&
xvalue = Trim$(Mid$(xstring,bpos&+blen&,epos&-bpos&-blen&))
foundstart = bpos&
Function = xvalue
End Function

Function xunwrap(ByVal xid As String, ByVal xstring As String, startpos As Long ) As String
Function = xunwrapext(xid,xstring,startpos,0)
End Function

'*******************************************************
'*
'*
'* UpdateX
'*
'*
'*******************************************************
Sub UpdateX (ByVal Tag As String,ByVal Value As String, X As String)
Dim startfound As Long
Dim p As Long
Dim Temp As String

p = 1
Temp = xunwrapext(Tag,x,p,startfound)
If Trim$(Temp) <> $BadParameter Then
   X = Mid$(X,1,startfound-1)+ xwrap(Tag,Trim$(value)) + Mid$(X,p)
Else
   X = x + xwrap(Tag,Trim$(value))
End If
End Sub


Function xGetv (ByVal selector As String, ByVal xstring As String) As Double

Dim xid As String
Dim xvalue As String

xid = selector

If xid= "" Then
    Function = Val(xstring)
Else
    xvalue = xUnWrap(xid,xString,1)
    If xvalue = $BadParameter Then
       Function = 0
    Else
       Function = Val(xvalue)
    End If
End If

End Function

Function xGets (ByVal selector As String, ByVal xstring As String) As String

Dim xid As String
Dim xvalue As String

xid = selector

If xid= "" Then
    Function = ""
Else
    xvalue = xUnWrap(xid,xString,1)
    If xvalue = $BadParameter Then
       Function = ""
    Else
       Function = xvalue
    End If
End If

End Function


Function xUnWrapTime(xid As String, ByVal Timex As String) As Ext
Dim xvalue As String
Dim evalue As Ext

' Try MJD
xid = $TagMJD
xvalue = xunwrap(xid,Timex,1)
If xvalue<>$BadParameter Then 'successful
     evalue = Val(xvalue)
    If evalue < Val($EarliestMJD) Then
        Function = %BadParameter
        Exit Function
    End If
    If evalue >= Val($LatestMJD)+1 Then
        Function = %BadParameter
        Exit Function
    End If
    Function = Val(xvalue)
    Exit Function
End If

' Try SDY
xid = $TagSDY
xvalue = xunwrap(xid,Timex,1)
If xvalue<>$BadParameter Then 'successful
    Function = Val(xvalue)
    Exit Function
End If

' Try UTD
xid = $TagUTD
xvalue = xunwrap(xid,Timex,1)
If xvalue<>$BadParameter Then 'successful
    Function = Val(xvalue)
    Exit Function
End If

' Try CTD
xid = $TagCTD
xvalue = xunwrap(xid,Timex,1)
If xvalue<>$BadParameter Then 'successful
    Function = Val(xvalue)
    Exit Function
End If

Function = %BadParameter
Exit Function


End Function






   